package com.idak.parser.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.idak.parser.model.Book;
import com.idak.parser.model.Dvd;
import com.idak.parser.service.XmlLibraryParser;

public class XmlLibraryParserImpl implements XmlLibraryParser {

	private XPath xPath;
	private Document xmlDocument;
	
	
	
	public XmlLibraryParserImpl() throws Exception {
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		builderFactory.setNamespaceAware(true);
		
		DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
		File file = new File(XmlLibraryParserImpl.class.getResource("/library.xml").getFile());
		xmlDocument = documentBuilder.parse(file);
		XPathFactory factory = XPathFactory.newInstance();
		xPath = factory.newXPath();
	}



	@Override
	public Book findByBookIsbn(String isbn) {
			Book book = new Book();
			String bookName;
			String bookSubject;
			List<String> bookAuthors = new ArrayList<String>(0);
		try {
			XPathExpression xPathExpression = xPath.compile("//LIBRARY//BOOKS//BOOK[@isbn='" +isbn + "']//NAME");
			bookName = xPathExpression.evaluate(xmlDocument, XPathConstants.STRING).toString();
			
			xPathExpression = xPath.compile("//LIBRARY//BOOKS//BOOK[@isbn='" +isbn + "']//SUBJECT");
			bookSubject = xPathExpression.evaluate(xmlDocument, XPathConstants.STRING).toString();
			
			xPathExpression = xPath.compile("//LIBRARY//BOOKS//BOOK[@isbn='" +isbn + "']//AUTHORS//AUTHOR");
			NodeList nodeListBook = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
			
			for(int index=0; index<nodeListBook.getLength();index++){			
				String author = nodeListBook.item(index).getTextContent();
				bookAuthors.add(author);
			}
            
			book.setIsbn(isbn);
			book.setName(bookName);
			book.setSubject(bookSubject);
			book.setAuthors(bookAuthors);
			
		} catch (XPathExpressionException e) {
		}
        
		return book;
	}



	@Override
	public Dvd findDvdById(String dvdId) {
		Dvd dvd = new Dvd();
		String dvdName;
		String dvdContent;
		List<String> authors = new ArrayList<String>(0);
		XPathExpression xPathExpression;
		
		try {
		    xPathExpression = xPath.compile("//LIBRARY//DVDS//DVD[@id='"+dvdId+"']//NAME");
			dvdName = xPathExpression.evaluate(xmlDocument, XPathConstants.STRING).toString();
			
			xPathExpression = xPath.compile("//LIBRARY//DVDS//DVD[@id='"+dvdId+"']//CONTENT");
			dvdContent = xPathExpression.evaluate(xmlDocument, XPathConstants.STRING).toString();
			
			xPathExpression = xPath.compile("//LIBRARY//DVDS//DVD[@id='"+dvdId+"']//AUTHORS/AUTHOR");
			
			NodeList authorsNodeList = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
			
			for(int index=0; index<authorsNodeList.getLength();index++){
				authors.add(authorsNodeList.item(index).getTextContent());
			}
			
			dvd.setAuthors(authors);
			dvd.setDvdId(dvdId);
			dvd.setName(dvdName);
			dvd.setContent(dvdContent);
			
		} catch (XPathExpressionException e) {
		}
		return dvd;
	}
	
	

}
